<template>
  <div>
    <el-form-item label="内容" prop="content">
      <mavon-editor
        ref="md"
        v-model="mdContent"
        :subfield="true"
        :autofocus="true"
        :toolbars="toolbars"
        @imgAdd="handleEditorImgAdd"
        @imgDel="handleEditorImgDel"
        @fullScreen="fullScreen($event)"
      />

    </el-form-item>
  </div>
</template>

<script>
import { upload } from '@/api/file'

//  npm install mavon-editor --save
import { mavonEditor } from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

export default {
  name: 'MarkdownEditor',
  components: {
    mavonEditor
  },
  props: {
    id: {
      type: String,
      default: ''
    },
    content: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imgFile: [],
      mdContent: this.$props.content,
      toolbars: {
        bold: false, // 粗体
        italic: true, // 斜体
        header: true, // 标题
        underline: true, // 下划线
        strikethrough: true, // 中划线
        mark: true, // 标记
        superscript: true, // 上角标
        subscript: true, // 下角标
        quote: true, // 引用
        ol: true, // 有序列表
        ul: true, // 无序列表
        link: true, // 链接
        imagelink: true, // 图片链接
        code: true, // code
        table: true, // 表格
        fullscreen: true, // 全屏编辑
        readmodel: true, // 沉浸式阅读
        htmlcode: true, // 展示html源码
        help: true, // 帮助
        /* 1.3.5 */
        undo: true, // 上一步
        redo: true, // 下一步
        trash: true, // 清空
        save: false, // 保存（触发events中的save事件）
        /* 1.4.2 */
        navigation: true, // 导航目录
        /* 2.1.8 */
        alignleft: true, // 左对齐
        aligncenter: true, // 居中
        alignright: true, // 右对齐
        /* 2.2.1 */
        subfield: true, // 单双栏模式
        preview: true // 预览
      }
    }
  },
  watch: {
    mdContent(val) {
      this.$emit('getContent', val)
    }
  },

  methods: {
    setMdContent(val) {
      this.mdContent = val
    },
    fullScreen(isFullScreen) {
      this.$emit('handleFullScreen', isFullScreen)
    },
    handleEditorImgAdd(pos, file) {
      // 当前域名
      const domain = process.env.VUE_APP_BASE_HOST
      var formdata = new FormData()
      formdata.append('upload-file', file)
      this.imgFile[pos] = file
      upload(formdata).then(response => {
        if (response.code !== 0) {
          this.$notify.error({
            title: '上传失败',
            message: response.message,
            duration: 2000
          })
        } else {
          const url = domain + response.data.Path
          const name = file.name
          const content = this.mdContent
          // 将返回的url替换到文本原位置![...](0) -> ![...](url)
          if (content.includes(name)) {
            const oStr = `(${pos})`
            const nStr = `(${url})`
            const index = content.indexOf(oStr)
            const str = content.replace(oStr, '')
            const insertStr = (soure, start, newStr) => {
              return soure.slice(0, start) + newStr + soure.slice(start)
            }
            this.mdContent = insertStr(str, index, nStr)
            console.log('替换图片标签')
          }
        }
      })
    },
    handleEditorImgDel(pos) {
      delete this.imgFile[pos]
      console.log('删除图片')
      console.log(this.imgFile)
    }

  }
}
</script>

<style scoped>

</style>

